﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;
namespace WEBDT.Areas.Admin.Controllers
{
    public class DienThoaiController : BaseController<SanPham>
    {
        //
        // GET: /Admin/CTHoaDon/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public override ActionResult insert()
        {
            var model = new DienThoaiModel
            {
                tontai = false,
            };
            ViewData["maloai"] = new SelectList(repository.GetLoaiSPFromNhom(1), "maloai", "tenloai");
            ViewData["mansx"] = new SelectList(repository.Danhsach<NhaSX>(), "mansx", "tennsx");
            return View(model);
        }

        [HttpPost]
        public override ActionResult insert(FormCollection fr)
        {
            var ht = new DienThoaiModel
            {
               TenSP= fr.Get("tensp"),
               DanhGia = Convert.ToInt32(fr.Get("danhgia")),
               LinkImage = fr.Get("image"),
               MaLoai= Convert.ToInt32(fr.Get("maloai")),
               MaNSX = Convert.ToInt32(fr.Get("mansx")),
               DonGia = Convert.ToInt32(fr.Get("dongia")),
               SoLuong = Convert.ToInt32(fr.Get("soluong")),
               LuotXem = Convert.ToInt32(fr.Get("luotxem")),
               Status = Convert.ToInt32(fr.Get("status")),
               Selloff = Convert.ToInt32(fr.Get("selloff")),
               TimeBH = Convert.ToInt32(fr.Get("baohanh")),
               Mayanh = fr.Get("Mayanh"),
               quayphim = fr.Get("quayphim"),
               nghenhac = fr.Get("nghenhac"),
               xemphim = fr.Get("xemphim"),
               trochoi = fr.Get("trochoi"),
               loangoai = fr.Get("loangoai"),
               ghiam = fr.Get("ghiam"),
               ungdungvp = fr.Get("ungdungvp"),
               loainhacchuong = fr.Get("loainhacchuong"),
               tainhac = fr.Get("tainhac"),
               bonhotrong = fr.Get("bonhotrong"),
               thenhongoai = fr.Get("thenhongoai"),
               danhbadt = fr.Get("danhbadt"),
               tinnhan = fr.Get("tinnhan"),
               GPRS = fr.Get("GPRS"),
               bag = fr.Get("bag"),
               wifi = fr.Get("wifi"),
               gps = fr.Get("gps"),
               loaimh = fr.Get("loaimh"),
               kichthuocmh = fr.Get("kichthuocmh"),
               camung = fr.Get("camung"),
               loaipin = fr.Get("loaipin"),
               thoigiancho = fr.Get("thoigiancho"),
               thoigianthoai = fr.Get("thoigianthoai"),
               kichthuocmay = fr.Get("kichthuocmay"),
               trongluong = fr.Get("trongluong"),
               hedieuhanh = fr.Get("hedieuhanh"),
            };
            var model = new SanPham
            {
                TenSP= ht.TenSP,
                DanhGia= ht.DanhGia,
                LinkImage= ht.LinkImage,
                MaLoai =ht.MaLoai,
                MaNSX= ht.MaNSX,
                DonGia= ht.DonGia,
                SoLuong= ht.SoLuong,
                LuotXem = ht.LuotXem,
                Status= ht.Status,
                Selloff= ht.Selloff,
                TimeBH=ht.TimeBH,
                MaNhom= 1,
                Mota = ht.Mayanh + "*" + ht.quayphim + "*" + ht.nghenhac + "*" +
                        ht.xemphim + "*" +
                        ht.trochoi + "*" +
                        ht.loangoai + "*" +
                        ht.ghiam + "*" +
                        ht.ungdungvp + "*" +
                        ht.loainhacchuong + "*" +
                        ht.tainhac + "*" +
                        ht.bonhotrong + "*" +
                        ht.thenhongoai + "*" +
                        ht.danhbadt + "*" +
                        ht.tinnhan + "*" +
                        ht.GPRS + "*" +
                        ht.bag + "*" +
                        ht.wifi + "*" +
                        ht.gps + "*" +
                        ht.loaimh + "*" +
                        ht.kichthuocmh + "*" +
                        ht.camung + "*" +
                        ht.loaipin + "*" +
                        ht.thoigiancho + "*" +
                        ht.thoigianthoai + "*" +
                        ht.kichthuocmay + "*" +
                        ht.trongluong + "*" +
                        ht.hedieuhanh 
            };
            repository.Insert<SanPham>(model);
            return RedirectToAction("getAll");
        }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var ht = repository.SelectByID<SanPham>(id);
            string mota = ht.Mota;
            var arr = mota.Split('*');
            var model = new DienThoaiModel
            {
                MaSP= ht.MaSP,
                TenSP = ht.TenSP,
                DanhGia = ht.DanhGia,
                LinkImage = ht.LinkImage,
                MaLoai = ht.MaLoai,
                MaNSX = ht.MaNSX,
                DonGia = ht.DonGia,
                SoLuong = ht.SoLuong,
                LuotXem = ht.LuotXem,
                Status = ht.Status,
                Selloff = ht.Selloff,
                TimeBH = ht.TimeBH,
                
            };
            ViewData["maloai"] = new SelectList(repository.Danhsach<LoaiSP>(), "maloai", "tenloai");
            ViewData["mansx"] = new SelectList(repository.Danhsach<NhaSX>(), "mansx", "tennsx");
            return View(model);
        }

        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var ht = new DienThoaiModel
            {
                MaSP = Convert.ToInt32(fr.Get("masp")),
                TenSP = fr.Get("tensp"),
                DanhGia = Convert.ToInt32(fr.Get("danhgia")),
                LinkImage = fr.Get("image"),
                MaLoai = Convert.ToInt32(fr.Get("maloai")),
                MaNSX = Convert.ToInt32(fr.Get("mansx")),
                DonGia = Convert.ToInt32(fr.Get("dongia")),
                SoLuong = Convert.ToInt32(fr.Get("soluong")),
                LuotXem = Convert.ToInt32(fr.Get("luotxem")),
                Selloff = Convert.ToInt32(fr.Get("selloff")),
                TimeBH = Convert.ToInt32(fr.Get("baohanh")),
            };
            var model = repository.SelectByID<SanPham>(ht.MaSP);
            model.TenSP = ht.TenSP;
            model.DanhGia = ht.DanhGia;
            model.LinkImage = ht.LinkImage;
            model.MaLoai = ht.MaLoai;
            model.MaNSX = ht.MaNSX;
            model.DonGia = ht.DonGia;
            model.SoLuong = ht.SoLuong;
            model.LuotXem = ht.LuotXem;
            model.Selloff = ht.Selloff;
            model.Status = ht.Status;
            model.TimeBH = ht.TimeBH;
            
            repository.commitchanges();
            return RedirectToAction("getAll");
        }
    }
}
